import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useRef, useEffect } from "react";
import useExternal2 from "../../hooks/useExternal2";
import classes from "./googlePayBtn.module.less";
import { jsx as _jsx } from "react/jsx-runtime";
function GooglePayBtn() {
    var _useExternal2 = useExternal2("https://pay.google.com/gp/p/js/pay.js", {
        type: "js",
        keepWhenUnused: true
    }), googlepayStatus = _useExternal2.status;
    var paymentsClient = useRef(null);
    var wrapper = useRef(null);
    var googlepayConfig = useRef(null);
    var onPaymentAuthorized = function() {};
    var getPaymentsClient = function() {
        if (paymentsClient.current) return paymentsClient.current;
        paymentsClient.current = new window.google.payments.api.PaymentsClient({
            environment: "TEST",
            paymentDataCallbacks: {
                onPaymentAuthorized: onPaymentAuthorized
            }
        });
        return paymentsClient.current;
    };
    // 点击付款按钮
    var onGooglePaymentButtonClicked = function() {};
    var addGooglePayButton = function() {
        var paymentsClient = getPaymentsClient();
        var button = paymentsClient.createButton({
            onClick: onGooglePaymentButtonClicked
        });
        wrapper.current.appendChild(button);
    };
    //从paypal获取googlepay配置
    var getGooglePayConfig = function() {
        var _ref = _async_to_generator(function() {
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (!(googlepayConfig.current === null)) return [
                            3,
                            2
                        ];
                        return [
                            4,
                            window.paypal.GooglePay().config()
                        ];
                    case 1:
                        googlepayConfig.current = _state.sent();
                        _state.label = 2;
                    case 2:
                        return [
                            2,
                            googlepayConfig.current
                        ];
                }
            });
        });
        return function getGooglePayConfig() {
            return _ref.apply(this, arguments);
        };
    }();
    useEffect(function() {
        if (wrapper.current && googlepayStatus === "ready") {
            var paymentsClient = getPaymentsClient();
            getGooglePayConfig().then(function(param) {
                var allowedPaymentMethods = param.allowedPaymentMethods, apiVersion = param.apiVersion, apiVersionMinor = param.apiVersionMinor;
                paymentsClient.isReadyToPay({
                    allowedPaymentMethods: allowedPaymentMethods,
                    apiVersion: apiVersion,
                    apiVersionMinor: apiVersionMinor
                }).then(function(response) {
                    if (response.result) {
                        addGooglePayButton();
                    }
                }).catch(function(err) {
                    console.log("googlepay 不可用");
                });
            });
        }
    }, [
        googlepayStatus
    ]);
    return /*#__PURE__*/ _jsx("div", {
        ref: wrapper,
        className: classes.wrap
    });
}
export default GooglePayBtn;

 //# sourceMappingURL=index.js.map